
#ifndef EDITORWINDOW_H
#define EDITORWINDOW_H

#include <string>
#include <map>

#include "../MyWindow.h"
#include "../Camera.h"

#include "globals.h"

#include "FileDialog.h"

enum{ LEFT_TOP, RIGHT_TOP, LEFT_BOTTOM, RIGHT_BOTTOM };

void RunCommand(std::string);

class OpenDialog : public FileDialog{
public:
	OpenDialog():FileDialog("Open"){}
	virtual void Process(std::string file){
		RunCommand("LoadFile " + file);
	}
};

class SaveDialog : public FileDialog{
public:
	SaveDialog():FileDialog("Save"){}
	virtual void Process(std::string file){
		RunCommand("SaveFile " + file);
	}
};

class EditorWindow : public MyBaseWindow {

	std::vector<MyGLView> viewports_;
	std::vector<MyTypeMesh> view_names_;
	std::vector<Camera> cams_;
	std::vector<float> zoom_;
	bool move_mode_;
	int active_cam_;
	int last_active_cam_;

	int maximized_view_;

	float active_cam_move_[3];


	//MyButton* button_;
	//cSlot< EditorWindow, int > button_pressed_slot_;

	//MyWinFrame* toolbar_;

	//MyButton* button2_;

	MyLinearLayout right_panel_;

	MyButton box_brush_button_;
	cSlot< EditorWindow, int > create_box_brush_slot_;

	MyLinearLayout grid_step_layout_;
	MyButton decr_grid_step_button_;
	cSlot< EditorWindow, int > decr_grid_step_slot_;
	MyButton incr_grid_step_button_;
	cSlot< EditorWindow, int > incr_grid_step_slot_;

	MyLinearLayout selection_layout_;
	MyButton clear_selection_button_;
	cSlot< EditorWindow, int > clear_selection_slot_;
	MyButton delete_selection_button_;
	cSlot< EditorWindow, int > delete_selection_slot_;
	MyButton copy_selection_button_;
	cSlot< EditorWindow, int > copy_selection_slot_;

	MyLinearLayout rotate_brush_layout_;
	MyButton rotate_cw_button_;
	cSlot< EditorWindow, int > rotate_cw_slot_;
	MyButton rotate_ccw_button_;
	cSlot< EditorWindow, int > rotate_ccw_slot_;

	MyLinearLayout mode_layout_;
	MyButton select_mode_button_;
	cSlot< EditorWindow, int > select_mode_slot_;
	MyButton edit_mode_button_;
	cSlot< EditorWindow, int > edit_mode_slot_;
	MyButton tex_mode_button_;
	cSlot< EditorWindow, int > tex_mode_slot_;

	std::map< std::string, MyTypeMesh> type_meshes_;

	MyEditBox input_string_;
	MyButton submit_input_btn_;
	cSlot< EditorWindow, int > submit_input_slot_;


	//list< MyBaseElement > properties_list_;
	MyEditField ed_field_;
	MyLinearLayout properties_layout_;

	MyDropMenu file_menu_;
	MyDropMenu edit_menu_;
	MyLinearLayout menu_layout_;
	MyWinFrame dark_back_;

	bool cam_rot_enabled_;

	OpenDialog open_file_dlg_;
	SaveDialog save_file_dlg_;

public:

	cSlot< EditorWindow, int, float, float > right_click;
	cSlot< EditorWindow, int, bool > wheel_slot;

	//MyMainWindow(HWND window_handle, char flags ) : MyBaseWindow(window_handle, flags), cam_rot_enabled_(false){
	EditorWindow(std::string title, int size_x, int size_y, char flags);

	virtual int OnClick( float x, float y );

	virtual int OnRightClick( float x, float y );

	virtual int OnMove( float dx, float dy );

	virtual int OnRelease( float x, float y);

	virtual int OnKeyPress( std::vector< unsigned char >& keys );

	int OnWheelRot( bool dir );

	void SetupViewPort( int view ) {

		const int* v = viewports_[view].dims_px();
		
		g_render_state.current_cam = &cams_[ view ];

		g_render_state.current_cam->Perspective( 60.0f, (float)v[2]/v[3], 0.1f, 10000.0f );

		viewports_[view].SetActive();

	}
	
	void SetupAuxViewPort( int view ) {

		const int* v_px = viewports_[view].dims_px();

		float hor = 0.5f * v_px[2] * zoom_[ view ];
		float vert = 0.5f * v_px[3] * zoom_[ view ];

		cams_[ view ].Orthographic( -hor, hor, -vert, vert, 5.0f, 10000.0f );
		//cams_[ view ].Orthographic( -250.0f, 250.0f, -250.0f, 250.0f, 5.0f, 10000.0f );

		g_render_state.current_cam = &cams_[ view ];

		viewports_[ view ].SetActive();

		glLineWidth(2.0f);

		//glClearColor(0.0, 0.0, 0.0, 1.0);


	}

	void UpdateView( int view ) {

		const int* v_px = viewports_[view].dims_px();

		float hor = 0.5f * v_px[2] * zoom_[ view ];
		float vert = 0.5f * v_px[3] * zoom_[ view ];

		cams_[ view ].Orthographic( -hor, hor, -vert, vert, 5.0f, 10000.0f );
		cams_[ view ].UpdatePlanes();
		
	}

	int Quit(){
		g_quit = true;
		return 0;
	}

	void Connect(SignalSet& signals){
		MyBaseWindow::Connect(signals);
		signals.right_click_signal.Connect(right_click);
		signals.wheel_signal.Connect(wheel_slot);

		open_file_dlg_.Connect(signals);
		save_file_dlg_.Connect(signals);
	}

	void Apply(){
		MyBaseWindow::Apply();

		open_file_dlg_.MakeCurrent();
		open_file_dlg_.Draw();
		open_file_dlg_.Apply();

		save_file_dlg_.MakeCurrent();
		save_file_dlg_.Draw();
		save_file_dlg_.Apply();
	}

	void ZoomIn();
	void ZoomOut();

	void DrawGrid( int view );

	void UpdateActiveCam( float delta ) {

		if( active_cam_ != -1 )
		{
			cams_[ active_cam_ ].Move( active_cam_move_, delta );
		}

	}

	void UpdateActiveCamPlanes() {

		if( active_cam_ != -1 )
		{
			cams_[ active_cam_ ].UpdatePlanes();
		}

	}

	void Draw();

	int CreateBoxBrush();
	int DecrGridStep();
	int IncrGridStep();
	int ClearSelection();
	int RotateCW();
	int RotateCCW();
	int EditModeToggle();
	int SelectModeOn();
	int TextureModeOn();
	int SubmitInput();
	int DeleteSelected();
	int CopySelected();

	cSlot<EditorWindow, int> new_file_slot_;
	int NewFile();

	cSlot<EditorWindow, int> open_file_slot_;
	int OpenFile();

	cSlot<EditorWindow, int> save_file_slot_;
	int SaveFile();

	cSlot<EditorWindow, int> undo_slot_;
	int Undo();

	cSlot<EditorWindow, int> redo_slot_;
	int Redo();
};





#endif
